
var file_util = require('../public/file_util');
var path = require('path');
const Define = require('./ExportDefine');
const TSParser = require('./TSParser');
function TSExport() {

    this.parserList = []
}

TSExport.prototype.init = function (cur_path, copy_info) {
    this.copy_info = copy_info;
    this.cur_path = cur_path;
    var temp = path.join(cur_path, 'template', this.copy_info.lang, 'class' + this.copy_info.version + '.ts')
   
    // console.log(' temp ', temp)
    this.template = file_util.readFile(temp);
    this.functionTemplate = file_util.readFile(path.join(cur_path, 'template', this.copy_info.lang, 'function' + this.copy_info.version + '.ts'));
    var manager = path.join(cur_path, 'template', this.copy_info.lang, 'manager.ts')
    this.managerTemplate = file_util.readFile(manager)
}
TSExport.prototype.exportManager = function (export_dir, excelList) {

}
//导出表的名字常量，使用时使用常量，易于维护
TSExport.prototype.exportFileName = function (export_dir, excelList) {

    let data = 'export const JsonDefine = {\n\t'
    for (let j = 0; j < this.parserList.length; j++) {
        const parser = this.parserList[j];
        data += parser.fileNames + '\n\t'
    }
    data += '\n}'
    let p = path.join(export_dir, this.copy_info.lang, 'JsonDefine.ts')
    file_util.writeFile(p, data);
}
//导出数据表中定义的枚举值
TSExport.prototype.exportEnumDefine = function (export_dir, excelList) {
    let data = ''
    for (let j = 0; j < this.parserList.length; j++) {
        const parser = this.parserList[j];
        if (parser.enumDefine) {
            data += parser.enumDefine + '\n'
        }
    }
    let p = path.join(export_dir, this.copy_info.lang, 'EnumDefine.ts')
    file_util.writeFile(p, data);
}
//导出类型声明文件
TSExport.prototype.exportDeclare = function (export_dir, excelList) {
    let data = ''
    // let temp = ''
    for (let j = 0; j < this.parserList.length; j++) {
        const parser = this.parserList[j];

        if (parser.declareObj) {
            data += parser.declareObj + '\n'
        }
        // temp += parser.fileNames + '\n\t'
    }
    // data += temp;
    let p = path.join(export_dir, this.copy_info.lang, 'json.d.ts')

    // console.log('ClassDefine data ', data)
    file_util.writeFile(p, data);
}

TSExport.prototype.exportClassFile = function (export_dir, excelList) {
    let class_path = path.join(export_dir, this.copy_info.lang, Define.CLASS_FOLDER);
    file_util.createDir(class_path)
    let moduleFolders = {}
    for (let j = 0; j < this.parserList.length; j++) {
        const parser = this.parserList[j];
        if (parser.classObj) {
            let maPath = class_path;
            let moduleFolder = parser.getModuleName();
            if (moduleFolder) {
                let folder = moduleFolders[moduleFolder]
                if (!folder) {
                    folder = path.join(class_path, moduleFolder)
                    moduleFolders[moduleFolder] = folder;
                    file_util.createDir(folder)

                }
                maPath = folder;

            }
            file_util.writeFile(path.join(maPath, parser.className + ".ts"), parser.classObj);
        }
    }

}

TSExport.prototype.parse = function (excelList) {
    for (let index = 0; index < excelList.length; index++) {
        const excel = excelList[index];
        let sheetList = excel.getSheetList(this.copy_info.filter);
        for (let j = 0; j < sheetList.length; j++) {
            const sheet = sheetList[j];
            let parser = new TSParser(sheet)
            parser.parse(this.managerTemplate, this.template, this.functionTemplate);
            this.parserList.push(parser)
        }
    }
}

TSExport.prototype.export = function (export_dir, excelList) {
    file_util.createDir(path.join(export_dir, this.copy_info.lang))
    this.parse(excelList);
    this.exportEnumDefine(export_dir, excelList)
    this.exportFileName(export_dir, excelList)
    this.exportDeclare(export_dir, excelList)
    this.exportClassFile(export_dir, excelList)
}

TSExport.prototype.copy = function (moduleName, export_path) {
    //将json文件复制到项目中
    let data_dest_path = this.copy_info.data_dest_path
    if (data_dest_path) {
        var json_path = path.join(export_path, Define.CLIENT_FOLDER)
        var dest_json_path = data_dest_path
        //拷贝数据
        file_util.createDir(dest_json_path)

        file_util.copyDir(json_path, dest_json_path)
    }

    let lang_dest_path = this.copy_info.lang_dest_path;
    if (lang_dest_path) {
        //拷贝语言文件
        var dest_lang_path = lang_dest_path
        file_util.createDir(dest_lang_path)
        // var langFile = moduleName + "_lang_zh.json"
        // console.log('copy_path  ===', data_dest_path, " json_path ", json_path, 'langFile', langFile)
        // file_util.copy(path.join(export_path,Define.LANG_PATH, langFile), path.join(dest_lang_path, langFile))
        file_util.copyDir(path.join(export_path,Define.LANG_PATH),dest_lang_path)
    }

    let d_ts_path = this.copy_info.d_ts_path;
    if (d_ts_path) {
        file_util.createDir(d_ts_path)
        var d_ts_file = "json.d.ts"
        // console.log('copy_path  ===', data_dest_path, " json_path ", json_path, 'd_ts_file', d_ts_file)
        file_util.copy(path.join(export_path, this.copy_info.lang, d_ts_file), path.join(d_ts_path, d_ts_file))
    }
    let enum_dest_path = this.copy_info.enum_dest_path;
    if (enum_dest_path) {
        file_util.createDir(enum_dest_path)
        var d_ts_file = "EnumDefine.ts"
        file_util.copy(path.join(export_path, this.copy_info.lang, d_ts_file), path.join(enum_dest_path, d_ts_file))
        var fileName = 'JsonDefine.ts'
        file_util.copy(path.join(export_path, this.copy_info.lang, fileName), path.join(enum_dest_path, fileName))
    }
}

module.exports = TSExport;